<template>
	<view class="container">
		<view class="header">
			<view class="title">現有積分</view>
			<view class="num">{{ user.balance }}</view>
			<image :src="$getOSSImg('mall_bg.png')" mode="aspectFill"></image>
		</view>
		<view class="mall-title">禮品專區</view>
		<view class="mall-list flex-wrap">
			<view v-for="item in listData" :key="item.id" class="mall-item flex-center">
				<image :src="item.imggoods" mode="aspectFill"></image>
				<view class="name">{{ item.name }}</view>
				<view class="price">{{ item.point }}</view>
				<view class="btn flex-center" @click="handleExchange(item)">立即兌換</view>
			</view>
		</view>
		<!-- 登录弹窗 -->
		<login-pop />
		<!-- 协议弹窗 -->
		<protocol-pop />
	</view>
</template>

<script>
	import CourseRow from '@/components/Course/row'
	import { queryBtPointMall, addBtPointMallOrder } from '@/api/course'
	export default {
		components: {
			CourseRow
		},
		data() {
			return {
				listParams: {
					sort: 'sorder',
					sortType: 'desc',
					pageNum: 1,
					pageSize: 10
				},
				listData: [],
				loadData: {
					status: 'loading'
				}
			}
		},
		async onLoad() {
			this.initPage()
		},
		methods: {
			async getList() {
				const { pageNum, pageSize } = this.listParams
				const { data } = await queryBtPointMall(this.listParams, { needLoading: false })
				if (data.result.length < pageSize) {
					this.loadData.status = 'nomore'
				}
				this.listParams.pageNum++
				this.listData = this.listData.concat(data.result)
			},
			async initPage() {
				this.listData = []
				Object.assign(this.listParams, {
					pageNum: 1
				})
				this.loadData.status = 'loading'
				await this.getList()
			},
			async handleExchange(row) {
				uni.showModal({
					title: '溫馨提示',
					content: `確認兌換${row.name}嗎`,
					success: async(r) => {
						if (r.confirm) {
							await addBtPointMallOrder({ id: row.id })
							this.$Message.success('兌換成功！')
							this.$store.dispatch('updateUserInfo')
						}
					}
				})
			}
		},
		onReachBottom() {
			if (this.loadData.status === 'loading') {
				this.getList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #fff;
		box-sizing: border-box;
		padding-top: 20rpx;
	}
	.header {
		position: relative;
		width: 700rpx;
		height: 220rpx;
		margin: 0 auto;
		image {
			position: absolute;
			top: 0;
			left: 0;
			width: 700rpx;
			height: 220rpx;
		}
		.title {
			position: absolute;
			top: 34rpx;
			left: 40rpx;
			z-index: 99;
			color: #fff;
			font-size: 34rpx;
		}
		.num {
			position: absolute;
			top: 50%;
			transform: translateY(-50%);
			z-index: 99;
			text-align: center;
			width: 100%;
			color: #fff;
			font-size: 60rpx;
			font-weight: bold;
		}
	}
	.mall-title {
		padding: 60rpx 25rpx 	40rpx;
		color: #333;
		font-size: 34rpx;
	}
	.mall-list {
		padding: 0 25rpx;
		.mall-item {
			flex-direction: column;
			margin-right: 80rpx;
			&:nth-of-type(n+3) {
				margin-right: 0;
			}
			image {
				width: 180rpx;
				height: 180rpx;
				margin-bottom: 16rpx;
			}
			.name {
				color: #333;
				font-size: 24rpx;
				padding-bottom: 6rpx;
			}
			.price {
				color: #333;
				font-size: 24rpx;
			}
			.btn {
				width: 180rpx;
				height: 60rpx;
				margin-top: 16rpx;
				border: 1px solid #E9930A;
				color: #E9930A;
				font-weight: 400;
				font-size: 28rpx;
				border-radius: 16rpx;
			}
		}
	}
</style>